*This file replicates regression results for VAR. 
*The code used in this file is adapted from Baker, Bloom, and Davis (2016) (created by Nick Bloom) to generate results related to Environmental Policy Uncertainty.

clear all
set more off

************************************
************************************
global months=1 //1 for monthly, 3 for quarterly
global lags=3/$months
global lags2=2*$lags
global jump=6
global plot=36/$months
************************************
************************************

*Basic data set-up
use data/modern_epu_data
keep year month total*
ren total_index_UP total_index_PU
foreach var in E P U EP EU PU EPU {
ren total_index_`var' `var'
}
save data/modern_epu_data_clean,replace

*Historical data
use data/all_hist_data
order year month quarter
cap drop _m
so year month quarter
merge 1:1 year month using data/modern_epu_data_clean,update
drop _m
so year month quarter
merge m:1 year quarter using data/abcq,update
drop if _m==2
drop _m
keep year - rgnp72

*Michigan consumer sentiment data - some robustness test data
merge 1:1 year month using data/consumer_sentiment_data
ren Month mich
drop if _m==2
drop _m

***Merge in govt data
merge m:1 year using data/yearly_fed_measurements
drop if _merge==2
drop _merge

***Merge in NIPA data
merge 1:1 year month using data/nipa
drop if _merge==2
drop _merge

*Extrapolate back using old GDP data
gen lgdp=log(gdp)
gen lrgnp=log(rgnp)
reg lgdp lrgnp
predict hlgdp
replace lgdp=hlgdp if lgdp==.

***Merge in Stock data
merge m:1 year month using data/sp_500_data
drop if _merge==2
drop _merge

***Merge in Macro data 
merge m:1 year month using data/macro_data
drop if _merge==2
drop _merge
replace vix=. if year<1990

***Merge historic IP
merge m:1 year month using data/ip
drop _merge
replace indus=ip
drop ip

***Merge historic FFR
merge m:1 year month using data/nyrate
drop if _merge==2
drop _merge
replace fedf=nyrate if fedf==.&year<1960
drop nyrate

**Merge in historical employment data
cap drop _m
so year month
merge 1:1 year month using data/historical_emp
drop _m
merge m:1 year month using data/sp_500_vol_data
drop if _merge==2
drop _merge
drop sp_index
gen ym=year+(month-1)/12
replace quarter=floor((month+2)/3) if quarter==.
gen yq=year+(quarter-1)/4
gen time=ym*12
tsset time
gen lemp=log(employment)
gen lip=log(indus)
ren sp sp

*These two obs were missing so hand punch in
replace sp=1380 if year==2012&month==11
replace sp=1420 if year==2012&month==12
gen lsp=log(sp)
gen ldurable=log(durable)
gen lnondurable=log(nondurable)
gen lpce=log(pce)
gen lgross=log(gross)
gen lfixed=log(fixed)
gen lnonres=log(nonres)
gen lstruct=log(structures)
gen lequip=log(equip)
gen lresident=log(resident)
ren fedfundsrate ffr

lab var h_EPU "Historical Policy uncertainty index"
lab var sp "Closing value of S&P500 index"
lab var gdp "Real Gross Domestic Product (GDP)"
lab var vix "VIX index of implied equity volatility on the S&P500"
lab var EPU "Post 1985 EPU"
lab var ym "Year in fraction (i.e. 2010.5 is July 2010)"
lab var year "Year"
lab var month "Calendar Month"
lab var time "Indicator for Stata to tsset, definied as ym*12"
lab var employment "Aggregate non-farm employment"
lab var lemp "Log employment"
lab var lgdp "Log real gdp"
lab var lip "Log industrial production"
lab var lsp "Log stock index"
lab var ldurable "Log durable consumption"
lab var lnondurable "Log nondurable consumption"
lab var lpce "Log personal consumption expenditure"
lab var lgross "Log gross investment"
lab var lfixed "Log fixed investment"
lab var lnonres "Log non-residential investment"

gen yr_mth = ym(year, month)
merge m:1 yr_mth using data/04_Env_Policy_indices
keep if year<2015
drop if year==2014 & month==12

*Uncertainty measures
rename EPU APU // From this point forward, we will use 'APU' to refer to Economic Policy Uncertainty and 'epu' to refer to Environmental Policy Uncertainty for coding convenience.
gen epu=NB_EPU
gen eu=EU
ren NB_EP_excl_unc NB_EP 

*For Quarterly Collapse the Data
if $months==3 {
collapse epu NB_EP APU h_EPU_war lsp ffr lemp ldurable lnondurable lgdp lgross lfixed lnonres lstruct lequip lpce lip vix eu lresident year mich quarter,by(yq)
gen time=4*yq
tsset time
gen ym=yq 
}
gen linvest=lgross
gen lconsumption=lpce

if $months==1 {
*Note to save changing variable names too much call industrial production GDP for monthly runs on IP
replace lgdp=lip
}

tsset time
gen dlgdp=lgdp-l.lgdp
forval i=1(1)3 {
gen l`i'=l`i'.dlgdp
gen f`i'=f`i'.dlgdp
}

su epu if year==2005|year==2006
global epu_low=r(mean) //65.07
su epu if year==2010
global epu_high=r(mean) //258.70

gen recession=0
replace recession=1 if (yq>=1945)&(yq<=1945.76)
replace recession=1 if (yq>=1948.74)&(yq<=1949.76)
replace recession=1 if (yq>=1953.24)&(yq<=1954.26)
replace recession=1 if (yq>=1957.49)&(yq<=1958.26)
replace recession=1 if (yq>=1960.24)&(yq<=1961)
replace recession=1 if (yq>=1969.74)&(yq<=1970.76)
replace recession=1 if (yq>=1973.74)&(yq<=1975)
replace recession=1 if (yq>=1980)&(yq<=1980.51)
replace recession=1 if (yq>=1981.49)&(yq<=1982.76)
replace recession=1 if (yq>=1990.49)&(yq<=1991)
replace recession=1 if (yq>=2001)&(yq<=2001.76)
replace recession=1 if (yq>=2008)&(yq<=2009.26)

*For magnitudes note investment falls by 8.4% on average in post-WWII recessions 
reg lgross recession yq
reg lemp recession yq
sa var_env_220430,replace

*Baseline regressions
u var_env_220430,replace
irf set results_env_220430,replace
gen ym2=ym^2
qui var APU epu lsp ffr lemp lgdp if year>=1985,lags(1(1)$lags)
irf create emp1,step($plot) replace set(results_env_220430)

qui var epu APU lsp ffr lemp lgdp if year>=1985,lags(1(1)$lags)
irf create emp2,step($plot) replace set(results_env_220430)

qui var epu lsp ffr lemp lgdp if year>=1985,lags(1(1)$lags)
irf create emp3,step($plot) replace set(results_env_220430)

*************For VAR variables summary stats tables (**Table 2)
if $months==1 {
qui var APU epu lsp ffr lemp lgdp if year>=1985,lags(1(1)$lags)
su grossinvestment employment lsp ffr APU epu if e(sample) 
}
***********************
*Quarterly regressions including investment
cap qui var APU epu lsp ffr lgross lgdp if year>=1985,lags(1(1)$lags)
cap irf create invest1,step($plot) replace set(results_env_220430)

cap qui var epu APU lsp ffr lgross lgdp if year>=1985,lags(1(1)$lags)
cap irf create invest2,step($plot) replace set(results_env_220430)

cap qui var epu lsp ffr lgross lgdp if year>=1985,lags(1(1)$lags)
cap irf create invest3,step($plot) replace set(results_env_220430)

global tests="emp1 emp2 emp3 invest1 invest2 invest3"


******************************
******************************
*Plotting the graph
******************************
******************************
u results_env_220430.irf,replace
ren step _step
drop cirf coirf sirf  fevd sf* mse* dm cdm stddm
drop stdi* stdc* stdf* stds*

gen ir=impulse+response
drop impulse response
ren _step step

qui reshape wide oirf irf stdoirf,i(step irfname) j(ir) string

***THREE STEPS OF NORMALIZATION TO GET MAGNITUDES
****Normalizing the oirfs into the same units as irf
egen epuratio=max((step==0)*oirfepuepu),by(irfname)
replace oirfepulgdp   =oirfepulgdp/epuratio
replace oirfepulemp   =oirfepulemp/epuratio
cap replace oirfepulgross   =oirfepulgross/epuratio
replace stdoirfepulgdp =stdoirfepulgdp /epuratio
replace stdoirfepulemp =stdoirfepulemp /epuratio
cap replace stdoirfepulgross =stdoirfepulgross /epuratio

****Normalizing the oirfs into meaningful units, vol to a 15 unit shock if actual, stock-market to 5% and interest rates to 1%
global ratio=$epu_high-$epu_low //193.63 ~ 194
global se=1.645
foreach var in lgdp lemp lgross {
cap replace oirfepu`var'=oirfepu`var'*$ratio
cap replace stdoirfepu`var'=stdoirfepu`var'*$ratio
****Normalizing the impact into % unit for both out and employment
cap replace oirfepu`var'=oirfepu`var'*100
cap replace stdoirfepu`var'=stdoirfepu`var'*100
***Generate standard error bands
cap gen l`var'=oirfepu`var'-stdoirfepu`var'*$se
cap gen h`var'=oirfepu`var'+stdoirfepu`var'*$se
}
lab var step "year"
sa irf,replace

******PLOTTING OUTPUT
lab var step "Months"

if $months==1 {
***MAIN PAPER FIGURES
*MAIN RESULTS - Employment (%) (Figure 3)
scatter oirfepulemp llemp hlemp step if irfname=="emp1"&step<=$plot,c(l l l)   lp(l _ -) s(+ p p) legend(off) lw(medthick medium medium) lc(black cranberry cranberry) mc(black cranberry cranberry)  xlab(0($jump)$plot)  graphregion(color(white))
graph export 06_Emp_APU_EnvPU.png, replace height(600) width(900)

*Figure A3a
scatter oirfepulemp llemp hlemp step if irfname=="emp2"&step<=$plot,c(l l l)   lp(l _ -) s(+ p p) legend(off) lw(medthick medium medium) lc(black cranberry cranberry) mc(black cranberry cranberry)  xlab(0($jump)$plot)  graphregion(color(white))
graph export 06_Emp_EnvPU_APU.png, replace height(600) width(900)

*Figure A3b
scatter oirfepulemp llemp hlemp step if irfname=="emp3"&step<=$plot,c(l l l)   lp(l _ -) s(+ p p) legend(off) lw(medthick medium medium) lc(black cranberry cranberry) mc(black cranberry cranberry)  xlab(0($jump)$plot)  graphregion(color(white))
graph export 06_Emp_EnvPUonly.png, replace height(600) width(900)

foreach var in emp1 emp2 emp3{
estpost sum oirfepulemp if irfname=="`var'"
esttab using "06_VAR_troughs.xls", cells("min") title(`var')  append
}
}
*************************

else {
*QUARTERLY MAIN RESULTS - Investment (%) (Figure 3) THIS ONLY PRODUCES SENSIBLE RESULTS WHEN MONTHS=3 IS SET AT THE BEGINNING
lab var step "Quarters"
scatter oirfepulgross llgross hlgross step if irfname=="invest1"&step<=$plot,c(l l l)   lp(l _ -) s(+ p p) legend(off) lw(medthick medium medium) lc(black cranberry cranberry) mc(black cranberry cranberry)  xlab(0($jump)$plot)    graphregion(color(white))
graph export 06_Inv_APU_EnvPU.png, replace height(600) width(900)

*Figure A3a
scatter oirfepulgross llgross hlgross step if irfname=="invest2"&step<=$plot,c(l l l)   lp(l _ -) s(+ p p) legend(off) lw(medthick medium medium) lc(black cranberry cranberry) mc(black cranberry cranberry)  xlab(0($jump)$plot)    graphregion(color(white))
graph export 06_Inv_EnvPU_APU.png, replace height(600) width(900)

*Figure A3b
scatter oirfepulgross llgross hlgross step if irfname=="invest3"&step<=$plot,c(l l l)   lp(l _ -) s(+ p p) legend(off) lw(medthick medium medium) lc(black cranberry cranberry) mc(black cranberry cranberry)  xlab(0($jump)$plot)    graphregion(color(white))
graph export 06_Inv_EnvPU_only.png, replace height(600) width(900)

foreach var in invest1 invest2 invest3{
estpost sum oirfepulgross if irfname=="`var'"
esttab using "06_VAR_troughs.xls", cells("min") title(`var')  append
}
}
